Ext.define("Ext.ux.MaceTree",{
		requires:[
			'Ext.data.TreeStore'
		],
		extend:'Ext.tree.TreePanel',
		xtype:'macetree',
		layout:'fit',
		
		initComponent:function(){
			var initialConfig=this.initialConfig,
			metadata=initialConfig["metaData"],
			attrs=this.attrs=metadata.attrs;
			this.dataUrl=attrs[""];
			this.saveUrl=attrs["saveUrl"]+"/"+metadata["id"];
			this.fields=metadata.fields;
			this.buildNameFieldMap();
			this.refEnumMap=metadata.refEnumMap;
			this.refModelMap=metadata.refModelMap;ss
			Ext.apply(this,this.attrs)
			this.initTreeConfig();
			this.callParent();
		},
		
		loadMaceMap:function(maceMap){
			var me=this;
			if(maceMap!=null&&Ext.isObject(maceMap)){
				me.store.cascade({
					'before':function(node){
						var id=node.data.id;
						if(maceMap[id]){
							node.setChecked(true);
						}else{
							node.setChecked(false);
						}
					},
					'after':function(node){
						
					}
				})
			}
		},
		
		buildNameFieldMap:function(){
			var me=this,nameFieldMap=this.nameFieldMap={},
			needKeyConvert=this.needKeyConvert=false,
			defaultValueMap=this.defaultValueMap={};
			Ext.iterate(this.fields,function(field,i){
				var name=field["name"];
				nameFieldMap[name]=field;
				if(Ext.isString(name)&&name.indexOf(".")>-1){
					needKeyConvert=true;
				}
				var defaultValue=field["attrs"]["defaultValue"];
				if(defaultValue){
					defaultValueMap[field["name"]]=defaultValue;
				}
			});
			this.fillDefaultValue();
		},
		
		fillDefaultValue:function(){
			var moduleIdField=this.nameFieldMap[this.moduleProperty];
			if(moduleIdField){
				var attrs=moduleIdField.attrs=moduleIdField.attrs||{};
				attrs["required"]=true;
				attrs["defaultValue"]=this.metaData["id"];
			}
		},
		
		
		initTreeTBar:function(){
			var me=this,
			
			tbar=[{
				text:"提交",
				iconCls:"fa fa-save fa-fw",
				handler:function(){
					me.showAddNodeWin();
				}
			},{
				text:"刷新",
				iconCls:"fa fa-refresh fa-fw",
				handler:function(){
					this.up('treepanel').store.reload();
				}
			}];
			return tbar;
			
		},
		
		refreshSelectedNode:function(){
			var me=this,tree=Ext.getCmp(me.treeCmpId),sels=tree.getSelection(),
			store=tree.store;
			if(sels.length==0){
				return store.reload();
			}
			var sel=sels[0],id=sel.get("id");
			me.refreshNode(id);
		},
		refreshNode:function(id){
			var me=this,tree=Ext.getCmp(me["treeCmpId"]),
	    	store=tree.store;
	    	var node = store.getNodeById(id),fid=node.raw.fid,
	    	fidNode=store.getNodeById(fid);
	    	store.load({node: fidNode, callback:function() {
	           var fidNode = store.getNodeById(fid);
	            tree.getSelectionModel().select(node, true, true);
	            fidNode.expand();
	        }, scope: this});
		},
		
		reloadNode:function(id){
			var me=this,tree=Ext.getCmp(me["treeCmpId"]),
	    	store=tree.store;
	    	var node = store.getNodeById(id);
	    	store.load({node: node, callback:function() {
	           var currentNode = store.getNodeById(node.raw.id);
	            tree.getSelectionModel().select(node, true, true);
	            node.expand();
	        }, scope: this});
		},
		
		buildTreeModel:function(){
			var me=this;
			return new Ext.data.TreeModel({
				 clientIdProperty: 'clientId',
				 identifier: 'negative',
				 idProperty:me.idProperty
			});
			
		},
		initTreeStore:function(){
			var me=this,action=me["action"],
			treeModel=this.buildTreeModel(),
			query=attrs["action"]||{type:"rest",url:""},
			queryUrl=query["url"];

			var proxy=this.buildProxy();
			
			var store= {
				autoDestroy :true,
				autoLoad :true,
				autoSync:true,
				proxy:proxy,
				defaultRootId:attrs["rootId"],
				defaultRootText:attrs["rootText"]||metadata["text"],
				parentIdProperty:me.parentIdProperty,
				model:treeModel,
				nodeParam:me.idProperty,
				listeners:{
					'load':function ( view, records, successful, operation, node, eOpts ) {
						if(records&&records.length>0){
							Ext.iterate(records,function(record,i){
								record.set('checked',false);
							});
						}
					}
				}
			};
			return store;
		},
		buildProxy:function(){
			var attrs=this.attrs,
			action=attrs["action"],
			query=this.query=this.query||action,
			create=this.create=this.create||action,
			update=this.update=this.update||action,
			destroy=this.destroy=this.destroy||action,
			api={},actionMethods={},proxy={};
			if(action&&action.type=="rest"){
				proxy["type"]="rest";
				proxy["url"]=action["url"];
				proxy["appendId"]=true;
			}else{
				proxy["type"]="ajax";
				api["create"]=create.url;
				actionMethods["create"]=create.method||"POST";
				api["query"]=query.url;
				actionMethods["query"]=query.method||"GET";
				api["destroy"]=destroy.url;
				actionMethods["destroy"]=destroy.method||"GET";
				api["update"]=update["url"];
				actionMethods["update"]=update["method"]||"POST";
				proxy["url"]=query.url||action["url"];
				proxy["api"]=api;
				proxy["appendId"]=true;
				proxy["actionMethods"]=actionMethods;
			}
			proxy["reader"]={
				type:action["format"]||"json",
				rootProperty:'children',
				typeProperty:attrs["typeProperty"]
			};
			return proxy;
		},
		
		
		initTreeConfig:function(region){
			var me=this,store=me.initTreeStore(),
			tbar=me.initTreeTBar(),
			listeners={
				'itemclick':function ( view, record, item, index, e, eOpts ) {
				var id=record.get(me["idProperty"]);
				
				
			},
			'selectionchange' :function(view, selected, eOpts ){
				
			}};
			return {
					width:330,
					height:'auto',
					id:me.treeCmpId,
					store:store,
					tbar:tbar,
					rootVisible:false,
					listeners:listeners
			};
			
		}
	});
